package com.guigu.qingcheng.servicegoods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.qingcheng.pojo.goods.TbCategory;
import com.guigu.qingcheng.servicegoods.mapper.TbCategoryMapper;
import com.guigu.qingcheng.servicegoods.service.TbCategoryService;
import com.guigu.qingcheng.utils.CacheKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品类目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class TbCategoryServiceImpl extends ServiceImpl<TbCategoryMapper, TbCategory> implements TbCategoryService {

    @Resource
    private TbCategoryMapper mapper;

    @Autowired
    private TbCategoryService tbCategoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    //根据条件查询封装查询对象
    public QueryWrapper<TbCategory> queryWrapper(Map<String, Object> searchMap){
        QueryWrapper<TbCategory> wapper = new QueryWrapper<TbCategory>();
        if(searchMap != null){
            if(searchMap.get("id") != null && !"".equals(searchMap.get("id")))
                wapper.eq("id", searchMap.get("id"));
            if(searchMap.get("name") != null && !"".equals(searchMap.get("name")))
                wapper.like("name", searchMap.get("name"));
            if(searchMap.get("goodsNum") != null && !"".equals(searchMap.get("goodsNum")))
                wapper.eq("goods_num", searchMap.get("goodsNum"));
            if(searchMap.get("isShow") != null && !"".equals(searchMap.get("isShow")))
                wapper.eq("is_show", searchMap.get("isShow"));
            if(searchMap.get("isMenu") != null && !"".equals(searchMap.get("isMenu")))
                wapper.eq("is_menu", searchMap.get("isMenu"));
            if(searchMap.get("seq") != null && !"".equals(searchMap.get("seq")))
                wapper.eq("seq", searchMap.get("seq"));
            if(searchMap.get("parentId") != null && !"".equals(searchMap.get("parentId")))
                wapper.eq("parent_id", searchMap.get("parentId"));
            if(searchMap.get("templateId") != null && !"".equals(searchMap.get("templateId")))
                wapper.eq("template_id", searchMap.get("templateId"));
        }
        return wapper;
    }
    @Override
    public Page<TbCategory> findPage(Map<String, Object> searchMap, int page, int size) {
        Page<TbCategory> pageCategory = new Page<TbCategory>(page, size);
        QueryWrapper<TbCategory> query = queryWrapper(searchMap);
        IPage<TbCategory> list = this.tbCategoryService.page(pageCategory, query);
        return pageCategory;
    }

    @Override
    public List<TbCategory> findList(Map<String, Object> searchMap) {
        QueryWrapper<TbCategory> wrapper = queryWrapper(searchMap);
        List<TbCategory> list = this.tbCategoryService.list(wrapper);
        return list;
    }

    /**
     * 查询前台主页菜单
     * @return
     */

    @Override
    public void delete(Integer id) {
        QueryWrapper<TbCategory> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty("id"))
        wrapper.eq("parent_id",id);
        int count = this.tbCategoryService.count(wrapper);
        if (count>0){
            throw new RuntimeException("存在下级分类不能删除");
        }
        saveCategoryTreeToRedis();
    }

    /**
     * 返回分类树
     * @return
     */
    @Override
    public List<Map> findCategoryTree() {
        //从缓存中查询
        System.out.println("从缓存中提取");
        return (List<Map>)redisTemplate.boundValueOps(CacheKey.CATEGORY_TREE.toString()).get();
    }

    @Override
    public void saveCategoryTreeToRedis() {
        System.out.println("加载商品分类数据到缓存");
        QueryWrapper<TbCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("is_show", "1");
        List<TbCategory> categories = tbCategoryService.list(wrapper);
        List<Map> categoryTree =findByParentId(categories,0);
        redisTemplate.boundValueOps(CacheKey.CATEGORY_TREE.toString()).set(categoryTree);
    }

    public List<Map> findByParentId(List<TbCategory> listCategory, Integer parentId){
        List<Map> list = new ArrayList<>();
        for (TbCategory category:listCategory){
            if (category.getParentId() == parentId){
                Map map = new HashMap();
                map.put("name",category.getName());
                map.put("menus",findByParentId(listCategory,category.getId()));
                list.add(map);
            }
        }
        return list;
    }

    /**
     * 新增
     * @param category
     */
    public Boolean add(TbCategory category) {
        Boolean f = tbCategoryService.save(category);
        saveCategoryTreeToRedis();
        return f;
    }
    /**
     * 修改
     * @param category
     */
    public Boolean update(TbCategory category) {
        Boolean f = tbCategoryService.updateById(category);
        saveCategoryTreeToRedis();
        return f;
    }

}
